<?php

declare(strict_types=1);

namespace app\admin\service;

use app\admin\model\CustomerInvoicebasicinfo as CustomerInvoicebasicinfoModel;

class CustomerInvoicebasicinfo extends \think\Service
{

    protected $return;
    protected $logger;
    protected $validate;
    protected $model;
    public function __construct()
    {
        $this->model = new CustomerInvoicebasicinfoModel();
        $this->return = [
            'code' => 500001,
            'data' => [],
            'message' => ''
        ];
    }

    /**
     * 注册服务
     *
     * @return mixed
     */
    public function register()
    {
        //
    }

    /**
     * 执行服务
     *
     * @return mixed
     */
    public function boot()
    {
        //
    }

    /**
     * 创建客户开票信息
     * @param array $data 客户开票信息的数据
     */
    public function create($data)
    {
        try {
            $exists = $this->model::where([
                'customer_id' => $data['customer_id'],
                'tax_id_num' => $data['tax_id_num'],
                'is_del' => 0
            ])->find();

            if ($exists) {
                return ['code' => 200001, 'message' => '纳税识别号已存在'];
            }
            $res = $this->model::create($data);
            if ($res) {
                // $this->logger->logging('创建', $res['id'], '手动创建');
                return ['code' => 0];
            } else {
                return ['code' => 500001];
            }
        } catch (\Throwable $th) {
            return ['code' => 500001, 'message' => '服务器内部错误：' . $th->getMessage()];
        }
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2025-02-07 17:24:24 
     * @param [type] @required
     * @return Renderable
     */
    public function getList(int $page = 1, int $limit = 10, array $where = [], $field = "*")
    {
        try {
            $query =  $this->model->where($where);
            $total = $query->count();

            if ($total <= 0) {
                $return['code'] = 0;
            } else {
                $list = $query
                    ->field($field)
                    ->page($page, $limit)
                    ->order('id desc')
                    ->select()
                    ->toArray();



                // $ownerArr =  array_filter(array_unique(array_column($list, 'owner')), function ($value) {
                //     return $value !== null;
                // });

                // $ownerList = $this->model->name('operator')->whereIn('id', $ownerArr)->column('id,cname', 'id');
                // foreach ($list as &$item) {
                //     $item['owner'] = $ownerList[$item['owner']]['cname'] ?? '';
                // }

                $return['code'] = 0;
                $return['data'] = ['list' => $list, 'total' => $total];
            }
        } catch (\Throwable $th) {
            $return['message'] = 'Failed to retrieve list. ' . $th->getMessage();
            $return['code'] = 500001;
        }
        return $return;
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2025-02-07 17:41:36 
     * @param [type] @required
     * @return Renderable
     */
    public function update($where, $data)
    {
        if (!$where) {
            return ['code' => 200001, 'message' => '缺少必要参数'];
        }
        $res = $this->model->where($where)->find();

        if (!$res) {
            return  ['code' => 400000, 'message' => '未找到对应数据'];;
        }
        foreach ($data as $key => $value) {
            if ($res->$key != $value) {
                $res->$key = $value;
            }
        }

        $exists = $this->model->where('is_del', 0)->where('id', '<>', $res->id)->where([
            'customer_id' => $data['customer_id'],
            'tax_id_num' => $data['tax_id_num'],
            'is_del' => 0
        ])->find();

        if ($exists) {
            return ['code' => 200001, 'message' => '纳税识别号已存在'];
        }

        $result = $res->save();

        if ($result) {
            return ['code' => 0];
        } else {
            return ['code' => 500001];
        }
    }


    /**
     * @describe 批量逻辑删除
     * @author: Bobo
     * @date: 2025-02-08 12:02:32 
     * @param [arr] @required $ids ID数组
     * @return Renderable
     */
    public function batchLogicalDelete($ids)
    {
        $this->model::startTrans();
        try {

            $this->model::where('id', 'in', $ids)->update(['is_del' => time()]);
            $this->model::commit();
            $return['code'] = 0;
        } catch (\Throwable $th) {
            $this->model::rollback();
            $return['code'] = 500001;
            $return['message'] = $th->getMessage();
        }
        return $return;
    }
}
